package com.spreadk.webdemo.controller;

import com.spreadk.webdemo.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:Gan Linghao
 * @date:2021/5/30
 * @time:15:26
 * @description:no description
 */
@WebServlet(name = "Index", urlPatterns = "/index")
public class IndexController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tmp = req.getParameter("option");
        Integer option = Integer.parseInt(tmp);

        switch (option) {
            case 1:
                login(req,resp);
                break;
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String pass = req.getParameter("password");
        System.out.println(name + "-" + pass);
        if(name.equals("admin")){
            if(pass.equals("123456")){
                List<User> users = new ArrayList<>();
                for (int i=0; i<10; i++) {
                    User user = new User();
                    user.setId(String.valueOf(i));
                    user.setAddress("某大道" + i);
                    user.setName("里斯" + i);
                    users.add(user);
                }
                // 换成jdbc响应数据库的方式取数据
                req.setAttribute("userList", users);
                //跳到用户管理列表页面
                req.getRequestDispatcher("/page/list.jsp").forward(req,resp);
            }else{
                // 跳回登录页面
                req.getRequestDispatcher("/index.jsp").forward(req,resp);
            }
        }
    }
}
